<route lang="json5">
{
  style: {
    navigationBarTitleText: '员工添加/编辑',
  },
}
</route>

<template>
    <item title="员工名称" required>
        <wd-input v-model="obj.name" clearable />
    </item>
    <item title="员工手机号" required>
        <wd-input v-model="obj.mobile" clearable />
    </item>
    <item title="验证码" required :tips="obj.id ? '不修改手机号时不用填写' : ''">
        <view class="code_box">
            <wd-input v-model="obj.code" clearable type="number" class="code_input" />
            <view class="code" @click="send()">{{ timer }}</view>
        </view>
    </item>
    <item title="员工权限" required>
        <wd-checkbox-group v-model="auths" style="text-align: left;">
            <wd-checkbox shape="square" :modelValue="item.ename" v-for="item, index in auth" :key="index">{{
                item.cname
            }}</wd-checkbox>
        </wd-checkbox-group>
    </item>

    <bottom_button title="提交" :fixed="false" class="submit" @click="submit()" />
</template>

<script lang="ts" setup>
import { useStore } from '@/utils/store';
import { error, filter, SmsCode } from '@/utils'
import { http } from '@/utils/http';
const store = useStore()
let auth = [
    { cname: '商品管理', ename: 'goods', auth: false },
    { cname: '线上购物订单', ename: 'online_order', auth: false },
    { cname: '到店核销订单', ename: 'offline_order', auth: false },
    { cname: '订单核销', ename: 'verification', auth: false },
    { cname: '员工管理', ename: 'worker', auth: false },
]
const obj = ref({
    id: 0,
    name: '',
    mobile: '',
    store_id: store.store && store.store.id ? store.store.id : 0,
    code: '',
    uuid: '',
})
const auths = ref([])
const timer = ref('发送验证码')
function send() {
    SmsCode(obj.value.mobile, timer).then(res => {
        if (res.code == 200) obj.value.uuid = res.data.uuid
    })
}

function submit() {
    if (!error([
        [!obj.value.name, '请输入店铺名称'],
        [!obj.value.mobile, '请输入法人会员手机号'],
        [(!obj.value.id && !obj.value.code), '请输入验证码'],
        [(!obj.value.id && !obj.value.uuid), '请发送验证码'],
    ])) return

    let param = JSON.parse(JSON.stringify(obj.value))
    param.code = parseInt(param.code)
    // 设置权限
    for (let i in auth) {
        if (auths.value.includes(auth[i].ename)) {
            auth[i].auth = true
        } else {
            auth[i].auth = false
        }
    }
    param.auth = auth

    http.post('/member/store/store_worker', param).then((res: any) => {
        uni.showToast({ title: res.message, icon: "none" })
        if (res.code == 200) {
            setTimeout(() => {
                uni.navigateBack({ delta: 1 })
            }, 1200);
        }
    })
}

onLoad((options) => {
    if (options.id) {
        obj.value.id = options.id
        http.get('/member/store/store_worker', { id: options.id }).then((res: any) => {
            if (res.code == 200) {
                auths.value = []
                for (let i in res.data.auth) {
                    if (res.data.auth[i].auth) auths.value.push(res.data.auth[i].ename)
                }
                filter(res.data, obj.value)
            }
        })
    }
})
</script>

<style scoped>
.code_box {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.code_input {
    width: calc(100% - 180rpx);
}

.code {
    width: 180rpx;
    margin-left: 20rpx;
    font-size: 26rpx;
    line-height: 35px;
    text-align: center;
    border: solid 1px #e5e5e5;
    border-radius: 10rpx;
}
</style>